package com.mybatis.sqlsession;

import com.mybatis.config.MyConfiguration;

import java.lang.reflect.Proxy;

/**
 * SqlSession会话
 * Configuration和Executor之间的桥梁
 */
public class SqlSession {

    private SqlExecutor executor = new MySqlExecutor();
    private final static MyConfiguration myConfiguration = new MyConfiguration();

    // 查
    public <T> T select(String statement, Object[] parameters, Class<?> returnType, Class<?> resultObject) {
        return executor.select(statement, parameters, returnType, resultObject);
    }

    // 增
    public <T> int insert(T pojo) {
        return executor.insert(pojo);
    }

    /**
     * 通过动态代理获取Mapper对象
     *
     * @param clazz 接口类
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public <T> T getMapper(Class<T> clazz) {
        return (T) Proxy.newProxyInstance(
                clazz.getClassLoader(),
                new Class[]{clazz},
                new MyMapperProxy(myConfiguration, this));
    }

}
